const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const $ = db.command.aggregate
const _ = db.command
const ZXGameCian = db.collection('ZX-Game-Cian')
const ZXGameHouse = db.collection('ZX-Game-House')
const ZXGameUser = db.collection('ZX-Game-User')
const nowDate = db.serverDate()

// 获取openId云函数入口函数
exports.main = async (event, context) => {
    process.env.TZ = 'Asia/Shanghai'

    let api = event.api
    let params = {}
    let _id = event.data._id
    let name = event.data.name
    let sect = event.data.sect
    let cianId = event.data.cianId
    let houseId = event.data.houseId

    if(api=='api-gameuser-edit'){
        // 判断是新增还是修改
        if(_id){
            delete event.data._id
            delete event.data.cianName
            delete event.data.houseName
            delete event.data.gameId
            event.data.hp = Number(event.data.hp)
            event.data.mp = Number(event.data.mp)
            return await ZXGameUser.doc(_id).update(event.data)
        }else{
            // 先查询是否已经存在游戏名称
            if(name)
                params.name = name
            const total = (await ZXGameUser.where(params).count()).total
            if(total){
                return {
                    type:'error',
                    msg:'游戏名称已存在！',
                }
            }else{
                event.data.crearTime = nowDate
                event.data.hp = Number(event.data.hp)
                event.data.mp = Number(event.data.mp)
                return await ZXGameUser.add(event.data)
            }
        }
    }

    if(api=='api-gameuser-list'){
        if (name)
            params.name = db.RegExp({
                regexp: String(name),
                options: 'i',
            })
        if (sect)
            params.sect = sect
        if (cianId)
            params.cianId = cianId
        if (houseId)
            params.houseId = houseId

        const res = await db
            .collection('ZX-Game-User')
            .aggregate()
            .lookup({
                from: 'ZX-Game-Cian',
                localField: 'cianId',
                foreignField: '_id',
                as: 'cianList'
            })
            .replaceRoot({
                newRoot: $.mergeObjects([$.arrayElemAt(['$cianList', 0]), '$$ROOT']),
            })
            .lookup({
                from: 'ZX-Game-House',
                localField: 'houseId',
                foreignField: '_id',
                as: 'houseList'
            })
            .replaceRoot({
                newRoot: $.mergeObjects([$.arrayElemAt(['$houseList', 0]), '$$ROOT'])
            })
            .project({
                cianList: 0,
                houseList: 0,
                // cianId: 0,
                // houseId: 0,
            })
            .match(params)
            .sort({
                "hp": -1,
                "mp": -1,
            })
            .end()
        return res.data
    }

    if(api=='api-gameuser-del'){
        if(_id){
            return await ZXGameUser.doc(_id).remove()
        }
    }
};